package com.lanjiyin.cloud.admin.rest;


import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.lanjiyin.cloud.admin.biz.MenuBiz;
import com.lanjiyin.cloud.admin.biz.UserBiz;
import com.lanjiyin.cloud.admin.entity.Menu;
import com.lanjiyin.cloud.admin.entity.User;
import com.lanjiyin.cloud.admin.rpc.service.PermissionService;
import com.lanjiyin.cloud.admin.vo.FrontUser;
import com.lanjiyin.cloud.admin.vo.MenuTree;
import com.lanjiyin.cloud.common.constant.RestCodeConstants;
import com.lanjiyin.cloud.common.context.BaseContextHandler;
import com.lanjiyin.cloud.common.msg.Result;
import com.lanjiyin.cloud.common.rest.BaseController;


@RestController
@RequestMapping("user")
public class UserController extends BaseController<UserBiz,User> {
    @Autowired
    private PermissionService permissionService;
    @Autowired
    protected UserBiz userBiz;
    @Autowired
    private MenuBiz menuBiz;
    private BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(12);
    @RequestMapping(value = "/front/info", method = RequestMethod.GET)
    @ResponseBody
    public Result<?> getUserInfo(String token) throws Exception {
        FrontUser userInfo = permissionService.getUserInfo(token);
        if(userInfo==null) {
            return new Result<String>(RestCodeConstants.TOKEN_ERROR_CODE,"登录状态失效，请重新登录！");
        } else {
            return new Result<FrontUser>(userInfo);
        }
    }
    @RequestMapping(value = "/pwd",method = RequestMethod.PUT)
    @ResponseBody
    public Result<String> updatePwd(@RequestBody User entity) throws Exception{
    	String userId = BaseContextHandler.getUserID();
    	User userInfo = userBiz.selectById(Integer.parseInt(userId));
    	if(userInfo==null) {
    		return new Result<String>(RestCodeConstants.TOKEN_ERROR_CODE,"登录状态失效，请重新登录！");
        } else {
        	entity.setId(userInfo.getId());
        	entity.setUsername(userInfo.getUsername());
        	if(StringUtils.isNotBlank(entity.getPassword())&&StringUtils.isNotBlank(entity.getAttr1())) {
        		if (encoder.matches(entity.getPassword(), userInfo.getPassword())) {
            		entity.setPassword(encoder.encode(entity.getAttr1()));
            		entity.setAttr1(null);
            		userBiz.updateSelectiveById(entity);
        		}else {
        			return new Result<String>(RestCodeConstants.TOKEN_PASSWORD_CODE,"登录密码不正确！");
        		}
        	}else {
        		return new Result<String>(RestCodeConstants.PARAM_ERROR_CODE,"参数有误！");
        	}
        }
        return new Result<String>();
    }

    @RequestMapping(value = "/front/menus", method = RequestMethod.GET)
    public @ResponseBody
    Result<List<MenuTree>> getMenusByUsername(String token) throws Exception {
        return new Result<List<MenuTree>>(permissionService.getMenusByUsername(token));
    }

    @RequestMapping(value = "/front/menu/all", method = RequestMethod.GET)
    public @ResponseBody
    Result<List<Menu>> getAllMenus() throws Exception {
    	Result<List<Menu>> result = new Result<List<Menu>>(menuBiz.selectListAll());
    	System.out.println(result);
        return result;
    }
    
    @PostMapping("login")
    public Result<Object> login(String username,String password){
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        
        return new Result<>(200, "登录成功");
        
        
    }
    
    
}
